package com.ruoyi.caipiao.service.impl;

import java.util.*;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.caipiao.domain.CaipiaoPl5History;
import com.ruoyi.caipiao.service.ICaipiaoPl5HistoryService;
import com.ruoyi.caipiao.utils.MapValueSortUtil;
import com.ruoyi.caipiao.utils.Pl3NumUtil;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.caipiao.mapper.CaipiaoPl3MethodFiveMapper;
import com.ruoyi.caipiao.domain.CaipiaoPl3MethodFive;
import com.ruoyi.caipiao.service.ICaipiaoPl3MethodFiveService;

/**
 * 方法5Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-12
 */
@Slf4j
@Service
public class CaipiaoPl3MethodFiveServiceImpl extends ServiceImpl<CaipiaoPl3MethodFiveMapper, CaipiaoPl3MethodFive> implements ICaipiaoPl3MethodFiveService {
    @Autowired
    private CaipiaoPl3MethodFiveMapper caipiaoPl3MethodFiveMapper;

    /**
     * 查询方法5
     *
     * @param id 方法5主键
     * @return 方法5
     */
    @Override
    public CaipiaoPl3MethodFive selectCaipiaoPl3MethodFiveById(Long id) {
        return caipiaoPl3MethodFiveMapper.selectCaipiaoPl3MethodFiveById(id);
    }

    /**
     * 查询方法5列表
     *
     * @param caipiaoPl3MethodFive 方法5
     * @return 方法5
     */
    @Override
    public List<CaipiaoPl3MethodFive> selectCaipiaoPl3MethodFiveList(CaipiaoPl3MethodFive caipiaoPl3MethodFive) {
        return caipiaoPl3MethodFiveMapper.selectCaipiaoPl3MethodFiveList(caipiaoPl3MethodFive);
    }

    /**
     * 新增方法5
     *
     * @param caipiaoPl3MethodFive 方法5
     * @return 结果
     */
    @Override
    public int insertCaipiaoPl3MethodFive(CaipiaoPl3MethodFive caipiaoPl3MethodFive) {
        caipiaoPl3MethodFive.setCreateTime(DateUtils.getNowDate());
        return caipiaoPl3MethodFiveMapper.insertCaipiaoPl3MethodFive(caipiaoPl3MethodFive);
    }

    /**
     * 修改方法5
     *
     * @param caipiaoPl3MethodFive 方法5
     * @return 结果
     */
    @Override
    public int updateCaipiaoPl3MethodFive(CaipiaoPl3MethodFive caipiaoPl3MethodFive) {
        return caipiaoPl3MethodFiveMapper.updateCaipiaoPl3MethodFive(caipiaoPl3MethodFive);
    }

    /**
     * 批量删除方法5
     *
     * @param ids 需要删除的方法5主键
     * @return 结果
     */
    @Override
    public int deleteCaipiaoPl3MethodFiveByIds(Long[] ids) {
        return caipiaoPl3MethodFiveMapper.deleteCaipiaoPl3MethodFiveByIds(ids);
    }

    /**
     * 删除方法5信息
     *
     * @param id 方法5主键
     * @return 结果
     */
    @Override
    public int deleteCaipiaoPl3MethodFiveById(Long id) {
        return caipiaoPl3MethodFiveMapper.deleteCaipiaoPl3MethodFiveById(id);
    }

    @Autowired
    private ICaipiaoPl5HistoryService caipiaoPl5HistoryService;

    @Override
    public List<CaipiaoPl3MethodFive> generateCode(CaipiaoPl3MethodFive caipiaoPl3MethodFive) {

        if (caipiaoPl3MethodFive.getQs() == null) {
            throw new IllegalArgumentException("必须传预测期数");
        }
        if (DateUtil.beginOfDay(new Date()).before(caipiaoPl3MethodFive.getQs())) {
            throw new IllegalArgumentException("预测期数不能大于当前时间");
        }

        QueryWrapper<CaipiaoPl5History> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("qs" , Long.parseLong(DateUtil.format(caipiaoPl3MethodFive.getQs(), "yyyyMMdd")));
        queryWrapper.last("order by qs desc limit 1");
        CaipiaoPl5History one = caipiaoPl5HistoryService.getOne(queryWrapper);
        if (one == null) {
            throw new IllegalArgumentException("还未获取开奖号码，请先获取");
        }
        String kjNum = one.getKjNum().substring(0, 3);
        ArrayList<String> sss = getDdddd(kjNum);

        List<CaipiaoPl3MethodFive> adds = new ArrayList<>();

        for (int i = 0; i < 1000; i++) {
            Set<String> extracted = extracted(sss, kjNum);
            CaipiaoPl3MethodFive save = new CaipiaoPl3MethodFive();
            save.setId(IdUtil.getSnowflakeNextId());
            save.setQs(caipiaoPl3MethodFive.getQs());
            save.setNums(Pl3NumUtil.set2Str(extracted));
            save.setNumsSize(extracted.size());
            adds.add(save);
            log.info("方法5：{}:{}" , extracted.size(), extracted.contains(kjNum));
        }

        return adds;
    }

//    @Test
//    public void test() {
//        String kjNum = "894";
//        ArrayList<String> sss = getDdddd(kjNum);
//
//        TreeSet<String> set = new TreeSet<>();
//        for (int i = 0; i < 10; i++) {
//            Set<String> extracted = extracted(sss, kjNum);
//            set.addAll(extracted);
//        }
//
//        System.out.println(set.size() + ":" + set.contains(kjNum));
//
//        MyFileUtil.writeContentToFile("D:\\"+ DateUtil.format(new Date(),"yyyyMMdd") +".txt", set.size() + ":" + set.contains(kjNum) +":"+set.toString()+"\n", true);
//    }

    private Set<String> extracted(ArrayList<String> sss, String kjNum) {
        Set<String> set = get(sss, kjNum);
        log.info("方法5：size:{}, 是否中上期号码:{}" , set.size(), set.contains(kjNum));
        for (int i = 0; i < 15; i++) {
            set.retainAll(get(sss, kjNum));
            log.info("方法5：size:{}, 是否中上期号码:{}" , set.size(), set.contains(kjNum));
        }
        return set;
    }

    private Set<String> get(ArrayList<String> sss, String kjNum) {
        Set<String> set = new TreeSet<>();
        for (int k = 0; k < 8; k++) {
            for (int i = 0; i < 10000; i++) {
                String num = Pl3NumUtil.getNum();
                if (sss.contains(i + "")) {
                    set.add(num);
                }
            }
        }
        return set;
    }

    private ArrayList<String> getDdddd(String kjNum) {
        List<Map<String, Integer>> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            Map<String, Integer> m = getData(kjNum);
            list.add(m);
        }

        Map<String, Integer> totalMap = new HashMap<>();
        for (Map<String, Integer> stringIntegerMap : list) {
            for (Map.Entry<String, Integer> stringIntegerEntry : stringIntegerMap.entrySet()) {
                Integer value = stringIntegerEntry.getValue();
                String key = stringIntegerEntry.getKey();
                totalMap.merge(key, value, Integer::sum);
            }
        }

        List<String> strings = MapValueSortUtil.sortMapByValue(totalMap, "desc");


        //中奖出现次数最多的位置
        ArrayList<String> sss = new ArrayList<>(strings.subList(0, 400));
//        System.out.println(strings);
        return sss;
    }

    private Map<String, Integer> getData(String kjNum) {
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < 10000; i++) {
            String num = Pl3NumUtil.getNum();
            if (num.equals(kjNum)) {
                map.merge(i + "" , 1, Integer::sum);
            }
        }
        return map;
    }

    @Override
    public List<CaipiaoPl3MethodFive> checkCode(CaipiaoPl3MethodFive caipiaoPl3MethodFive) {

        List<CaipiaoPl3MethodFive> list = this.baseMapper.listNoCheckNum(caipiaoPl3MethodFive);
        for (CaipiaoPl3MethodFive pl3MethodFive : list) {
            if (pl3MethodFive.getNums().contains(pl3MethodFive.getKjNum().substring(0, 3))) {
                pl3MethodFive.setRightPl3(1);
            } else {
                pl3MethodFive.setRightPl3(2);
            }
        }
        return list;
    }
}
